package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.databinding.HzklActivityUserFeedbackBinding;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserFeedBackActivity extends BaseActivity {
    private HzklActivityUserFeedbackBinding binding;
    @Inject
    UserRepo repo;
    private static final int MAX_TEXT_LENGTH = 120;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_user_feedback);
        binding.setMaxTextLength(MAX_TEXT_LENGTH);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        //binding.resetBtn.setOnClickListener(view -> startActivity(new Intent(HZKLUserResetPwdActivity.this,HZKLUserNameActivity.class)));
        binding.backContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int a = s.length() - MAX_TEXT_LENGTH;
                if (a > 0) {
                    toast("最多输入" + MAX_TEXT_LENGTH + "字");
                    s.delete(MAX_TEXT_LENGTH, s.length());
                } else {
                    binding.setTextLength(s.length());
                }
            }
        });
        binding.hzklYes.setOnClickListener(v -> setBack());
        new EditTextChangeBoolean().buttonEnable(binding.backContent, enable -> binding.hzklYes.setEnabled(enable));
    }

    private void setBack() {
        String content = binding.backContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请输入反馈内容", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = binding.backPhoneLayout.getText().toString();
        repo.feedBack(content, phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Toast.makeText(this, "已收到反馈", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        });
    }
}
